import type { Query } from "@/interface/Query";
import { onMounted, ref } from 'vue'
import { listApi , operatorApi , oneId , deleteApi } from "@/controller/PostApi"
import type {Post} from "@/interface/Post";
import type {Rule} from "ant-design-vue/es/form";
import { message } from "ant-design-vue";

export default function () {

    let queryModel = ref({
        page: 1,
        size: 15
    } as Query);

    onMounted(()=> {
        getPosts();
    });

    let searchBox = ref(true);
    let tableData = ref([] as Post[]);
    let total = ref(0);
    function getPosts() {
        listApi(queryModel.value).then(res => {
            if (res.code == 200) {
                tableData.value = res.data.records;
                total.value = parseInt(res.data.totalRow);
            }
        });
    }

    function resetQuery() {
        queryModel.value = {
            page: 1,
            size: 15
        } as Query;
        getPosts();
    }

    const columns = [
        {
            title: '岗位名称',
            dataIndex: 'name',
            width: 120,
            fixed: 'left',
            resizable: true
        },
        {
            title: '岗位编码',
            dataIndex: 'code',
            width: 200,
            resizable: true
        },
        {
            title: '岗位描述',
            dataIndex: 'remark',
            width: 200,
            resizable: true
        },
        {
            title: '排序',
            dataIndex: 'sortOrder',
            width: 80,
            resizable: true
        },
        {
            title: '状态',
            dataIndex: 'status',
            width: 80,
            resizable: true
        },
        {
            title: '创建时间',
            dataIndex: 'createTime',
            resizable: true,
            width: 160
        },
        {
            title: '更新时间',
            dataIndex: 'updateTime',
            resizable: true,
            width: 160
        },
        {
            title: '操作',
            dataIndex: 'action',
            fixed: 'right',
            resizable: true,
            width: 160
        }
    ]

    function handleResizeColumn(w : any, col : any) {
        col.width = w;
    }

    function pageChange(page: number,size: number) {
        queryModel.value.page = page;
        queryModel.value.size = size;
        getPosts();
    }

    let post = ref({} as Post)

    let operatorStore = ref({
        cmd: 0,
        open: false
    });

    function operator(cmd : number , id? : string | null) {
        operatorStore.value.cmd = cmd;
        operatorStore.value.open = true;
        if(id) {
            oneId(id).then(res => {
                if(res.code == 200) {
                    post.value = res.data;
                }
            })
        }
    }

    let formRef = ref();
    function submitOperator() {
        formRef.value
            .validate()
            .then(() => {
                operatorApi(post.value).then(res => {
                    if(res.code == 200) {
                        message.success( operatorStore.value.cmd == 0 ? '新增岗位成功!' : '编辑岗位成功');
                        resetFrom();
                        getPosts();
                        operatorStore.value.open = false;
                    }else {
                        message.error(res.msg);
                    }
                });
            })
            .catch(( e: any ) => {

            });
    }


    let rules : Record<string, Rule[]> = {
        name: [{ required: true, message: '请输入岗位名称', trigger: 'blur' }],
        code: [{ required: true, message: '请输入岗位编码', trigger: 'blur' }],
        sortOrder: [{ required: true, message: '请输入排序', trigger: 'blur' }]
    }
    function resetFrom() {
        post.value = {} as Post;
    }


    function deletePost(ids : string[]) {
        deleteApi(ids).then(res => {
            if (res.code == 200) {
                message.success('删除岗位成功');
                getPosts();
                rowsData.value  = [] as  Post[];
            }else {
                message.error(res.msg);
            }
        })
    }

    let rowsData  = ref<Post[]>([])
    function selectData(selectedRowKeys : any, selectedRows : Post[]) {
        rowsData.value = selectedRows;
    }


    return {
        queryModel , searchBox , getPosts ,
        resetQuery , handleResizeColumn , columns ,
        tableData , total , pageChange , operatorStore ,
        operator , post , rules ,submitOperator , formRef ,
        resetFrom , deletePost , selectData , rowsData
    }
}
